package aop05;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;

/**
 * Author:shixiaojun@itany.com
 * Date:2022/5/18 13:29
 * Version:1.0-SNAPSHOT
 * Description:
 */
public class LogAdvice {

    public void before(JoinPoint jp){
        // 获取目标类
//        Object target = jp.getThis();
        Object target = jp.getTarget();
        // 目标方法
        Signature signature = jp.getSignature();
        // 参数列表
        Object[] args = jp.getArgs();
        System.out.println("前置通知:在"+new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss").format(new Date())+"执行了"+signature.getName()+"方法,方法的参数为:"+ Arrays.toString(args));
    }

    public void afterReturning(JoinPoint jp,Object returnValue){
        System.out.println("正常返回通知:"+jp.getSignature().getName()+"方法执行成功,方法的返回值为:"+returnValue);
    }

    public void afterThrowing(JoinPoint jp,Exception e){
        System.out.println("异常通知:"+jp.getSignature().getName()+"执行遇到了异常,异常为:"+e);
    }

    public void after(JoinPoint jp){
        System.out.println("后置通知:"+jp.getSignature().getName()+"执行结束");
    }

}
